﻿namespace singleton
{
    /// <summary>
    /// 双 if + lock 
    /// </summary>
    public class DoubleIfWithLockSingleton
    {
        private static DoubleIfWithLockSingleton _instance;
        private static object _locker = new();

        private DoubleIfWithLockSingleton()
        {
        }

        public static DoubleIfWithLockSingleton Instance()
        {
            // 保证实例化之后的线程不用等待锁，节省时间
            if (_instance == default)
            {
                // 保证只有一个线程进入
                lock (_locker)
                {
                    // 在锁中对象为空再实例化，保证只实例化一次
                    if (_instance == default)
                    {
                        _instance = new DoubleIfWithLockSingleton();
                    }
                }
            }
            return _instance;
        }
    }
}
